package com.seocoo.worker.base.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyProgressView extends View {
    private Paint mCircleOutPaint;
    private Paint mLinePaint;

    private Paint mDefaltCircleOutPaint;
    private Paint mDefaltLinePaint;
    private Paint mTvPaint;

    //圆点
    private int circleOutRadius = 16;

    private int margin = 10; //左右margin

    private float mProgress;

    private ValueAnimator animation;

    public MyProgressView(Context context) {

        super(context);
        initView();
    }


    public MyProgressView(Context context, AttributeSet attrs) {

        super(context, attrs);
        initView();
    }


    public MyProgressView(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);
        initView();
    }


    public void initView() {

        mDefaltCircleOutPaint = new Paint();
        mDefaltCircleOutPaint.setStyle(Paint.Style.FILL);
        mDefaltCircleOutPaint.setColor(Color.parseColor("#CECED0"));
        mDefaltCircleOutPaint.setAntiAlias(true);

        mDefaltLinePaint = new Paint();
        mDefaltLinePaint.setColor(Color.parseColor("#CECED0"));
        mDefaltLinePaint.setStrokeWidth(8);


        mTvPaint = new Paint();
        mTvPaint.setColor(Color.parseColor("#858585"));
        mTvPaint.setTextSize(sp2px(getContext(), 12));


        mCircleOutPaint = new Paint();
        mCircleOutPaint.setStyle(Paint.Style.FILL);
        mCircleOutPaint.setColor(Color.parseColor("#059D5A"));
        mCircleOutPaint.setAntiAlias(true);

        mLinePaint = new Paint();
        mLinePaint.setColor(Color.parseColor("#059D5A"));
        mLinePaint.setStrokeWidth(8);

    }


    public void setProgress(float progress) {

        if (animation == null) {
            animation = ValueAnimator.ofFloat(0f, Float.valueOf(progress));
        }
        animation.addUpdateListener(animation -> {

            mProgress = (float) animation.getAnimatedValue();
            invalidate();

        });

        animation.setDuration(3000);
        animation.start();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        getSuggestedMinimumWidth();
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    @Override


    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        int height = getHeight();

        int width = getWidth();

        //灰色线
        canvas.drawLine(margin + circleOutRadius * 2, height / 2, width, height / 2, mDefaltLinePaint);


        int realWidth = width / 7;
        //灰色圆点
        for (int i = 0; i < 6; i++) {
            canvas.drawCircle(margin + circleOutRadius * (i * 2 + 1) + realWidth * i, height / 2, circleOutRadius, mDefaltCircleOutPaint);
        }

        //文字
        canvas.drawText("0分", margin + circleOutRadius - getTextWidth("0分"), height / 2 + 50, mTvPaint);
        canvas.drawText("20分", margin + circleOutRadius * 3 + realWidth - getTextWidth("20分"), height / 2 + 50, mTvPaint);
        canvas.drawText("40分", margin + circleOutRadius * 5 + realWidth * 2 - getTextWidth("40分"), height / 2 + 50, mTvPaint);
        canvas.drawText("60分", margin + circleOutRadius * 7 + realWidth * 3 - getTextWidth("60分"), height / 2 + 50, mTvPaint);
        canvas.drawText("80分", margin + circleOutRadius * 9 + realWidth * 4 - getTextWidth("80分"), height / 2 + 50, mTvPaint);
        canvas.drawText("100分", margin + circleOutRadius * 11 + realWidth * 5 - getTextWidth("100分"), height / 2 + 50, mTvPaint);
        int size = 0;
        if (mProgress <= 0) {
            size = 0;
        } else if (mProgress <= 20) {
            size = 1;
        } else if (mProgress <= 40) {
            size = 2;
        } else if (mProgress <= 60) {
            size = 3;
        } else if (mProgress <= 80) {
            size = 4;
        } else if (mProgress < 100) {
            size = 5;
        } else {
            size = 6;
        }

        float result = (margin + circleOutRadius * 12 + realWidth * 5) / 100.0f * mProgress;
        result =  result == 0 ? margin + circleOutRadius * 2 : result;
        canvas.drawLine(margin + circleOutRadius * 2, height / 2, result, height / 2, mLinePaint);

        for (int i = 0; i < size; i++) {
            canvas.drawCircle(margin + circleOutRadius * (i * 2 + 1) + realWidth * i, height / 2, circleOutRadius, mCircleOutPaint);
        }

    }

    private float getTextWidth(String text) {

        return mTvPaint.measureText(text) / 2;
    }


    private static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

}
